package com.huluobo.hyj.hyjmodel.ui.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.adapter.AddfiendAdapter
import com.huluobo.hyj.hyjmodel.contract.addFriend.AddFriendContract
import com.huluobo.hyj.hyjmodel.contract.addFriend.AddFriendPresenter
import kotlinx.android.synthetic.main.activity_add_friend.*
import kotlinx.android.synthetic.main.fragment_conversation.recyclerView
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.toast

/**
 *@author : USER
 *@date : 2020/9/17 15:16
 *@params :
 *@description:
 */
class AddFristActivity : BaseActivity() ,AddFriendContract.View{

    private val Presenter = AddFriendPresenter(this)

    //显示布局
    override fun getLayoutResId(): Int = R.layout.activity_add_friend
    //初始化
    override fun init() {
        super.init()
        //显示添加好友
        headerTitle.text= getString(R.string.add_friend)
        //适配的配置
        recyclerView.apply {
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context) //显示方式
            adapter = AddfiendAdapter(context,Presenter.addFriendItems)//添加到适配
        }

        //发起搜索
        search.setOnClickListener {
            search()
        }
        userName.setOnEditorActionListener { _, _, _ ->
            search()
            true
        }
    }

    private fun search(){
        hideSoftKeyboard()
        showProgress(getString(R.string.searching))
        val key = userName.text.trim().toString()
        Presenter.search(key)
    }

    //成功
    override fun onSearchSuccess() {
        dismissProgress()
        toast(R.string.search_success)
        recyclerView.adapter?.notifyDataSetChanged()//刷新适配器
    }

    override fun onSearchFailed() {
        dismissProgress()
        toast(R.string.search_failed)
    }
}